from datetime import datetime, timedelta
from .config import pkgm_config


class UserData:
    def __init__(self,):
        self._user_db = {}

    def add_user(self, key: str, user_info: dict):
        current_time = datetime.now()
        self._user_db[key] = {"data": user_info, "time": current_time}

    def get_user(self, key: str):
        if key in self._user_db:
            expire_days = timedelta(days=pkgm_config.user_token_expire_days)
            time_difference = datetime.now() - self._user_db[key]["time"]
            if expire_days < time_difference:
                self._user_db.pop(key)
                return 401, "token expired"
            return 200, self._user_db[key]["data"]
        else:
            return 402, "token error"

    def remove_user(self, key: str):
        if key in self._user_db:
            self._user_db.pop(key)
            return 200, "success"
        else:
            return 402, "token error"


server_user_data = UserData()
